package com.swgk.core.download;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;

public class RandomAccessFileFactory {

    private static final String [] model = {"r","rw","rws","rwd"};

    /**
     * 创建以只读的方式打开文本
     * @param fileUrl
     * @return
     * @throws FileNotFoundException
     */
    public static RandomAccessFile getRAFWithModelR(File fileUrl) throws FileNotFoundException {
        RandomAccessFile raf = new RandomAccessFile(fileUrl, model[0]);
        return raf;
    }

    /**
     * 创建读和写都是允许操作
     * @param fileUrl
     * @return
     * @throws FileNotFoundException
     */
    public static RandomAccessFile getRAFWithModelRW(File fileUrl) throws FileNotFoundException {
        RandomAccessFile raf = new RandomAccessFile(fileUrl, model[1]);
        return raf;
    }

    /**
     * 创建每当进行写操作，同步的刷新到磁盘，刷新内容和元数据
     * @param fileUrl
     * @return
     * @throws FileNotFoundException
     */
    public static RandomAccessFile getRAFWithModelRWS(File fileUrl) throws FileNotFoundException {
        RandomAccessFile raf = new RandomAccessFile(fileUrl, model[2]);
        return raf;
    }

    /**
     * 创建每当进行写操作，同步的刷新到磁盘，刷新内容
     * @param fileUrl
     * @return
     * @throws FileNotFoundException
     */
    public static RandomAccessFile getRAFWithModelRWD(File fileUrl) throws FileNotFoundException {
        RandomAccessFile raf = new RandomAccessFile(fileUrl, model[3]);
        return raf;
    }
}
